package com.hp.bon.sgw.wh.channel.invoices;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.api.internal.util.StringUtils;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.service.UserResourceConfigService;

public class InvoiceConfigLoader {

    private static final Logger _log = LoggerFactory.getLogger(InvoiceConfigLoader.class);

    public BizParam doLoader(BizParam bizParam) {

        String requestPath = bizParam.getParamString(BizConsts.KEY_REQUEST_PATH);
        ;
        String requestMethod = bizParam.getParamString(BizConsts.KEY_REQUEST_METHOD);

        if (BizConsts.VALUE_PATH_MONTHLY_BILL.equals(requestPath)) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_monthlyBill");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_monthlyBill");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if (BizConsts.VALUE_PATH_PAYMENT.equals(requestPath)) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_payment");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_payment");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if (BizConsts.VALUE_PATH_ORDERONEITEM.equals(requestPath)) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_orderOneItem");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_orderOneItem");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if (BizConsts.VALUE_PATH_ISSUEINVOICE.equals(requestPath) && requestMethod.equalsIgnoreCase("POST")) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_invoice_post");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_invoice_post");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if (BizConsts.VALUE_PATH_INVOICE.equals(requestPath) && requestMethod.equalsIgnoreCase("GET")) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_invoice_get");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_invoice_get");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if (BizConsts.VALUE_PATH_INVOICEINFORMATION.equals(requestPath) && requestMethod.equalsIgnoreCase("GET")) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_invoiceInformation_get");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_invoiceInformation_get");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if (BizConsts.VALUE_PATH_INVOICEINFORMATION.equals(requestPath) && requestMethod.equalsIgnoreCase("POST")) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_invoiceInformation_post");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_invoiceInformation_post");
            } else {
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        } else if ((String.valueOf(requestPath).indexOf(BizConsts.VALUE_PATH_INVOICEINFORMATION)>=0) && requestMethod.equalsIgnoreCase("PATCH")) {
            String accountApiUrl = UserResourceConfigService.getValue("dzfp_invoiceInformation_patch");
            if (StringUtils.isEmpty(accountApiUrl)) {
                _log.error("none accountApiUrl of dzfp_invoiceInformation_patch");
            } else {
                _log.info("accountApiUrl {}",accountApiUrl);
                bizParam.setParam(BizConsts.KEY_ACCOUNT_API_URL, accountApiUrl);
            }
        }

        return bizParam;
    }

}
